home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr54 / bison118.zip / FILES.C < prev    next >
C/C++ Source or Header  |  1993-06-01  |  9KB  |  377 lines

  1. /* Open and close files for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #ifdef VMS
  22. #include <ssdef.h>
  23. #define unlink delete
  24. #ifndef XPFILE
  25. #define XPFILE "GNU_BISON:[000000]BISON.SIMPLE"
  26. #endif
  27. #ifndef XPFILE1
  28. #define XPFILE1 "GNU_BISON:[000000]BISON.HAIRY"
  29. #endif
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #include "system.h"
  34. #include "files.h"
  35. #include "new.h"
  36. #include "gram.h"
  37.  
  38. FILE *finput = NULL;
  39. FILE *foutput = NULL;
  40. FILE *fdefines = NULL;
  41. FILE *ftable = NULL;
  42. FILE *fattrs = NULL;
  43. FILE *fguard = NULL;
  44. FILE *faction = NULL;
  45. FILE *fparser = NULL;
  46.  
  47. /* File name specified with -o for the output file, or 0 if no -o.  */
  48. char *spec_outfile;
  49.  
  50. char *infile;
  51. char *outfile;
  52. char *defsfile;
  53. char *tabfile;
  54. char *attrsfile;
  55. char *guardfile;
  56. char *actfile;
  57. char *tmpattrsfile;
  58. char *tmptabfile;
  59.  
  60. extern char    *mktemp();    /* So the compiler won't complain */
  61. extern char    *getenv();
  62. extern void    perror();
  63. extern void    exit();
  64. FILE    *tryopen();    /* This might be a good idea */
  65. void done();
  66.  
  67. extern char *program_name;
  68. extern int verboseflag;
  69. extern int definesflag;
  70. int fixed_outfiles = 0;
  71.  
  72.  
  73. char*
  74. stringappend(string1, end1, string2)
  75. char *string1;
  76. int end1;
  77. char *string2;
  78. {
  79.   register char *ostring;
  80.   register char *cp, *cp1;
  81.   register int i;
  82.  
  83.   cp = string2;  i = 0;
  84.   while (*cp++) i++;
  85.  
  86.   ostring = NEW2(i+end1+1, char);
  87.  
  88.   cp = ostring;
  89.   cp1 = string1;
  90.   for (i = 0; i < end1; i++)
  91.     *cp++ = *cp1++;
  92.  
  93.   cp1 = string2;
  94.   while (*cp++ = *cp1++) ;
  95.  
  96.   return ostring;
  97. }
  98.  
  99.  
  100. /* JF this has been hacked to death.  Nowaday it sets up the file names for
  101.    the output files, and opens the tmp files and the parser */
  102. void
  103. openfiles()
  104. {
  105.   char *name_base;
  106.   register char *cp;
  107.   char *filename;
  108.   int base_length;
  109.   int short_base_length;
  110.  
  111. #ifdef VMS
  112.   char *tmp_base = "sys$scratch:b_";
  113. #else
  114.   char *tmp_base = "/tmp/b.";
  115. #endif
  116.   int tmp_len;
  117.  
  118. #ifdef MSDOS
  119.   tmp_base = getenv ("TMP");
  120.   if (tmp_base == 0)
  121.     tmp_base = "";
  122.   strlwr (infile);
  123. #endif /* MSDOS */
  124.  
  125.   tmp_len = strlen (tmp_base);
  126.  
  127.   if (spec_outfile)
  128.     {
  129.       /* -o was specified.  The precise -o name will be used for ftable.
  130.      For other output files, remove the ".c" or ".tab.c" suffix.  */
  131.       name_base = spec_outfile;
  132. #ifdef MSDOS
  133.       strlwr (name_base);
  134. #endif /* MSDOS */
  135.       /* BASE_LENGTH includes ".tab" but not ".c".  */
  136.       base_length = strlen (name_base);
  137.       if (!strcmp (name_base + base_length - 2, ".c"))
  138.     base_length -= 2;
  139.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  140.       short_base_length = base_length;
  141.       if (!strncmp (name_base + short_base_length - 4, ".tab", 4))
  142.     short_base_length -= 4;
  143.       else if (!strncmp (name_base + short_base_length - 4, "_tab", 4))
  144.     short_base_length -= 4;
  145.     }
  146.   else if (spec_file_prefix)
  147.     {
  148.       /* -b was specified.  Construct names from it.  */
  149.       /* SHORT_BASE_LENGTH includes neither ".tab" nor ".c".  */
  150.       short_base_length = strlen (spec_file_prefix);
  151.       /* Count room for `.tab'.  */
  152.       base_length = short_base_length + 4;
  153.       name_base = (char *) mallocate (base_length + 1);
  154.       /* Append `.tab'.  */
  155.       strcpy (name_base, spec_file_prefix);
  156.       strcat (name_base, ".tab");
  157. #ifdef MSDOS
  158.       strlwr (name_base);
  159. #endif /* MSDOS */
  160.     }
  161.   else
  162.     {
  163.       /* -o was not specified; compute output file name from input
  164.      or use y.tab.c, etc., if -y was specified.  */
  165.  
  166.       name_base = fixed_outfiles ? "y.y" : infile;
  167.  
  168.       /* BASE_LENGTH gets length of NAME_BASE, sans ".y" suffix if any.  */
  169.  
  170.       base_length = strlen (name_base);
  171.       if (!strcmp (name_base + base_length - 2, ".y"))
  172.     base_length -= 2;
  173.       short_base_length = base_length;
  174.  
  175. #ifdef VMS
  176.       name_base = stringappend(name_base, short_base_length, "_tab");
  177. #else
  178. #ifdef MSDOS
  179.       name_base = stringappend(name_base, short_base_length, "_tab");
  180. #else
  181.       name_base = stringappend(name_base, short_base_length, ".tab");
  182. #endif /* not MSDOS */
  183. #endif
  184.       base_length = short_base_length + 4;
  185.     }
  186.  
  187.   finput = tryopen(infile, "r");
  188.  
  189.   filename = getenv("BISON_SIMPLE");
  190. #ifdef MSDOS
  191.   /* File doesn't exist in current directory; try in INIT directory.  */
  192.   cp = getenv("INIT");
  193.   if (filename == 0 && cp != 0)
  194.     {
  195.       filename = malloc(strlen(cp) + strlen(PFILE) + 2);
  196.       strcpy(filename, cp);
  197.       cp = filename + strlen(filename);
  198.       *cp++ = '/';
  199.       strcpy(cp, PFILE);
  200.     }
  201. #endif /* MSDOS */
  202.   fparser = tryopen(filename ? filename : PFILE, "r");
  203.  
  204.   if (verboseflag)
  205.     {
  206. #ifdef MSDOS
  207.       outfile = stringappend(name_base, short_base_length, ".out");
  208. #else
  209.       if (spec_name_prefix)
  210.     outfile = stringappend(name_base, short_base_length, ".out");
  211.       else
  212.     outfile = stringappend(name_base, short_base_length, ".output");
  213. #endif
  214.       foutput = tryopen(outfile, "w");
  215.     }
  216.  
  217.   if (definesflag)
  218.     {
  219.       defsfile = stringappend(name_base, base_length, ".h");
  220.       fdefines = tryopen(defsfile, "w");
  221.     }
  222.  
  223. #ifdef MSDOS
  224.   actfile = mktemp(stringappend(tmp_base, tmp_len, "acXXXXXX"));
  225.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "atXXXXXX"));
  226.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "taXXXXXX"));
  227. #else
  228.   actfile = mktemp(stringappend(tmp_base, tmp_len, "act.XXXXXX"));
  229.   tmpattrsfile = mktemp(stringappend(tmp_base, tmp_len, "attrs.XXXXXX"));
  230.   tmptabfile = mktemp(stringappend(tmp_base, tmp_len, "tab.XXXXXX"));
  231. #endif /* not MSDOS */
  232.  
  233.   faction = tryopen(actfile, "w+");
  234.   fattrs = tryopen(tmpattrsfile,"w+");
  235.   ftable = tryopen(tmptabfile, "w+");
  236.  
  237. #ifndef MSDOS
  238.   unlink(actfile);
  239.   unlink(tmpattrsfile);
  240.   unlink(tmptabfile);
  241. #endif
  242.  
  243.     /* These are opened by `done' or `open_extra_files', if at all */
  244.   if (spec_outfile)
  245.     tabfile = spec_outfile;
  246.   else
  247.     tabfile = stringappend(name_base, base_length, ".c");
  248.  
  249. #ifdef VMS
  250.   attrsfile = stringappend(name_base, short_base_length, "_stype.h");
  251.   guardfile = stringappend(name_base, short_base_length, "_guard.c");
  252. #else
  253. #ifdef MSDOS
  254.   attrsfile = stringappend(name_base, short_base_length, ".sth");
  255.   guardfile = stringappend(name_base, short_base_length, ".guc");
  256. #else
  257.   attrsfile = stringappend(name_base, short_base_length, ".stype.h");
  258.   guardfile = stringappend(name_base, short_base_length, ".guard.c");
  259. #endif /* not MSDOS */
  260. #endif /* not VMS */
  261. }
  262.  
  263.  
  264.  
  265. /* open the output files needed only for the semantic parser.
  266. This is done when %semantic_parser is seen in the declarations section.  */
  267.  
  268. void
  269. open_extra_files()
  270. {
  271.   FILE *ftmp;
  272.   int c;
  273.   char *filename, *cp;
  274.  
  275.   fclose(fparser);
  276.  
  277.   filename = (char *) getenv ("BISON_HAIRY");
  278. #ifdef MSDOS
  279.   /* File doesn't exist in current directory; try in INIT directory.  */
  280.   cp = getenv("INIT");
  281.   if (filename == 0 && cp != 0)
  282.     {
  283.       filename = malloc(strlen(cp) + strlen(PFILE1) + 2);
  284.       strcpy(filename, cp);
  285.       cp = filename + strlen(filename);
  286.       *cp++ = '/';
  287.       strcpy(cp, PFILE1);
  288.     }
  289. #endif
  290.   fparser= tryopen(filename ? filename : PFILE1, "r");
  291.  
  292.         /* JF change from inline attrs file to separate one */
  293.   ftmp = tryopen(attrsfile, "w");
  294.   rewind(fattrs);
  295.   while((c=getc(fattrs))!=EOF)    /* Thank god for buffering */
  296.     putc(c,ftmp);
  297.   fclose(fattrs);
  298.   fattrs=ftmp;
  299.  
  300.   fguard = tryopen(guardfile, "w");
  301.  
  302. }
  303.  
  304.     /* JF to make file opening easier.  This func tries to open file
  305.        NAME with mode MODE, and prints an error message if it fails. */
  306. FILE *
  307. tryopen(name, mode)
  308. char *name;
  309. char *mode;
  310. {
  311.   FILE    *ptr;
  312.  
  313.   ptr = fopen(name, mode);
  314.   if (ptr == NULL)
  315.     {
  316.       fprintf(stderr, "%s: ", program_name);
  317.       perror(name);
  318.       done(2);
  319.     }
  320.   return ptr;
  321. }
  322.  
  323. void
  324. done(k)
  325. int k;
  326. {
  327.   if (faction)
  328.     fclose(faction);
  329.  
  330.   if (fattrs)
  331.     fclose(fattrs);
  332.  
  333.   if (fguard)
  334.     fclose(fguard);
  335.  
  336.   if (finput)
  337.     fclose(finput);
  338.  
  339.   if (fparser)
  340.     fclose(fparser);
  341.  
  342.   if (foutput)
  343.     fclose(foutput);
  344.  
  345.     /* JF write out the output file */
  346.   if (k == 0 && ftable)
  347.     {
  348.       FILE *ftmp;
  349.       register int c;
  350.  
  351.       ftmp=tryopen(tabfile, "w");
  352.       rewind(ftable);
  353.       while((c=getc(ftable)) != EOF)
  354.         putc(c,ftmp);
  355.       fclose(ftmp);
  356.       fclose(ftable);
  357.     }
  358.  
  359. #ifdef VMS
  360.   if (faction)
  361.     delete(actfile);
  362.   if (fattrs)
  363.     delete(tmpattrsfile);
  364.   if (ftable)
  365.     delete(tmptabfile);
  366.   if (k==0) sys$exit(SS$_NORMAL);
  367.   sys$exit(SS$_ABORT);
  368. #else
  369. #ifdef MSDOS
  370.   if (actfile) unlink(actfile);
  371.   if (tmpattrsfile) unlink(tmpattrsfile);
  372.   if (tmptabfile) unlink(tmptabfile);
  373. #endif /* MSDOS */
  374.   exit(k);
  375. #endif /* not VMS */
  376. }
  377.